#include "Comm.hpp"
//#include "Log.hpp"
const int flags = IPC_CREAT | IPC_EXCL;

int main()
{
    CreatFifo();
    int fd=Open(FIFO_FILE,READER);
       cout<<"fd: "<<fd<<endl;
    assert(fd>=0);
    key_t key = CreatKey();
    Log() << "key: " << key << "\n";
    Log() << "create share memory begin\n";
    int shmid = shmget(key, MEM_SIZE, flags | 0666);
    if (shmid < 0)
    {
        Log() << "shmget: " << strerror(errno) << endl;
        // shmctl(shmid,IPC_RMID,nullptr);
        cout << "9999999999999999999" << endl;
        return 2;
    }
    Log() << "create shm success, shmid: " << shmid << "\n";
    char *str = (char *)shmat(shmid, nullptr, 0);
    Log() << "attach shm : " << shmid << " success\n";
    while (true)
    {
        if(Wait(fd)<=0) break;
        cout << "..." << str << endl;
        sleep(1);
    }
    shmdt(str);
    Log() << "detach shm : " << shmid << " success\n";
    shmctl(shmid, IPC_RMID, nullptr);
    Log() << "delete shm : " << shmid << " success\n";
    Close(fd,FIFO_FILE);
    return 0;
}
